<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="金额" prop="amount">
        <el-input
          v-model="queryParams.amount"
          placeholder="请输入金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="振幅" prop="amplitude">
        <el-input
          v-model="queryParams.amplitude"
          placeholder="请输入振幅"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收盘价" prop="close">
        <el-input
          v-model="queryParams.close"
          placeholder="请输入收盘价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="流动市值" prop="flowMarketValue">
        <el-input
          v-model="queryParams.flowMarketValue"
          placeholder="请输入流动市值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最高价" prop="high">
        <el-input
          v-model="queryParams.high"
          placeholder="请输入最高价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上市日期" prop="listingDate">
        <el-input
          v-model="queryParams.listingDate"
          placeholder="请输入上市日期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最低价" prop="low">
        <el-input
          v-model="queryParams.low"
          placeholder="请输入最低价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开盘价" prop="open">
        <el-input
          v-model="queryParams.open"
          placeholder="请输入开盘价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="前日收盘价" prop="preClose">
        <el-input
          v-model="queryParams.preClose"
          placeholder="请输入前日收盘价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="stockName">
        <el-input
          v-model="queryParams.stockName"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="号码" prop="stockNum">
        <el-input
          v-model="queryParams.stockNum"
          placeholder="请输入号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总流通股数" prop="totalFlowShares">
        <el-input
          v-model="queryParams.totalFlowShares"
          placeholder="请输入总流通股数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总市值" prop="totalMarketValue">
        <el-input
          v-model="queryParams.totalMarketValue"
          placeholder="请输入总市值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总股数" prop="totalShares">
        <el-input
          v-model="queryParams.totalShares"
          placeholder="请输入总股数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="换手率" prop="turnOverrate">
        <el-input
          v-model="queryParams.turnOverrate"
          placeholder="请输入换手率"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="涨跌价格" prop="upDownPrices">
        <el-input
          v-model="queryParams.upDownPrices"
          placeholder="请输入涨跌价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="涨跌幅度" prop="upDownRange">
        <el-input
          v-model="queryParams.upDownRange"
          placeholder="请输入涨跌幅度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="三日涨跌幅度" prop="upDownRange3">
        <el-input
          v-model="queryParams.upDownRange3"
          placeholder="请输入三日涨跌幅度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="五日涨跌幅度" prop="upDownRange5">
        <el-input
          v-model="queryParams.upDownRange5"
          placeholder="请输入五日涨跌幅度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新日期" prop="updateDate">
        <el-input
          v-model="queryParams.updateDate"
          placeholder="请输入更新日期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交易数量" prop="volume">
        <el-input
          v-model="queryParams.volume"
          placeholder="请输入交易数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['stock:stock:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['stock:stock:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['stock:stock:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['stock:stock:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="stockList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="stockId" align="center" prop="stockId" />
      <el-table-column label="金额" align="center" prop="amount" />
      <el-table-column label="振幅" align="center" prop="amplitude" />
      <el-table-column label="收盘价" align="center" prop="close" />
      <el-table-column label="流动市值" align="center" prop="flowMarketValue" />
      <el-table-column label="最高价" align="center" prop="high" />
      <el-table-column label="上市日期" align="center" prop="listingDate" />
      <el-table-column label="最低价" align="center" prop="low" />
      <el-table-column label="开盘价" align="center" prop="open" />
      <el-table-column label="前日收盘价" align="center" prop="preClose" />
      <el-table-column label="名称" align="center" prop="stockName" />
      <el-table-column label="号码" align="center" prop="stockNum" />
      <el-table-column label="总流通股数" align="center" prop="totalFlowShares" />
      <el-table-column label="总市值" align="center" prop="totalMarketValue" />
      <el-table-column label="总股数" align="center" prop="totalShares" />
      <el-table-column label="换手率" align="center" prop="turnOverrate" />
      <el-table-column label="涨跌价格" align="center" prop="upDownPrices" />
      <el-table-column label="涨跌幅度" align="center" prop="upDownRange" />
      <el-table-column label="三日涨跌幅度" align="center" prop="upDownRange3" />
      <el-table-column label="五日涨跌幅度" align="center" prop="upDownRange5" />
      <el-table-column label="更新日期" align="center" prop="updateDate" />
      <el-table-column label="交易数量" align="center" prop="volume" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['stock:stock:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['stock:stock:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改股票信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="振幅" prop="amplitude">
          <el-input v-model="form.amplitude" placeholder="请输入振幅" />
        </el-form-item>
        <el-form-item label="收盘价" prop="close">
          <el-input v-model="form.close" placeholder="请输入收盘价" />
        </el-form-item>
        <el-form-item label="流动市值" prop="flowMarketValue">
          <el-input v-model="form.flowMarketValue" placeholder="请输入流动市值" />
        </el-form-item>
        <el-form-item label="最高价" prop="high">
          <el-input v-model="form.high" placeholder="请输入最高价" />
        </el-form-item>
        <el-form-item label="上市日期" prop="listingDate">
          <el-input v-model="form.listingDate" placeholder="请输入上市日期" />
        </el-form-item>
        <el-form-item label="最低价" prop="low">
          <el-input v-model="form.low" placeholder="请输入最低价" />
        </el-form-item>
        <el-form-item label="开盘价" prop="open">
          <el-input v-model="form.open" placeholder="请输入开盘价" />
        </el-form-item>
        <el-form-item label="前日收盘价" prop="preClose">
          <el-input v-model="form.preClose" placeholder="请输入前日收盘价" />
        </el-form-item>
        <el-form-item label="名称" prop="stockName">
          <el-input v-model="form.stockName" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="号码" prop="stockNum">
          <el-input v-model="form.stockNum" placeholder="请输入号码" />
        </el-form-item>
        <el-form-item label="总流通股数" prop="totalFlowShares">
          <el-input v-model="form.totalFlowShares" placeholder="请输入总流通股数" />
        </el-form-item>
        <el-form-item label="总市值" prop="totalMarketValue">
          <el-input v-model="form.totalMarketValue" placeholder="请输入总市值" />
        </el-form-item>
        <el-form-item label="总股数" prop="totalShares">
          <el-input v-model="form.totalShares" placeholder="请输入总股数" />
        </el-form-item>
        <el-form-item label="换手率" prop="turnOverrate">
          <el-input v-model="form.turnOverrate" placeholder="请输入换手率" />
        </el-form-item>
        <el-form-item label="涨跌价格" prop="upDownPrices">
          <el-input v-model="form.upDownPrices" placeholder="请输入涨跌价格" />
        </el-form-item>
        <el-form-item label="涨跌幅度" prop="upDownRange">
          <el-input v-model="form.upDownRange" placeholder="请输入涨跌幅度" />
        </el-form-item>
        <el-form-item label="三日涨跌幅度" prop="upDownRange3">
          <el-input v-model="form.upDownRange3" placeholder="请输入三日涨跌幅度" />
        </el-form-item>
        <el-form-item label="五日涨跌幅度" prop="upDownRange5">
          <el-input v-model="form.upDownRange5" placeholder="请输入五日涨跌幅度" />
        </el-form-item>
        <el-form-item label="更新日期" prop="updateDate">
          <el-input v-model="form.updateDate" placeholder="请输入更新日期" />
        </el-form-item>
        <el-form-item label="交易数量" prop="volume">
          <el-input v-model="form.volume" placeholder="请输入交易数量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStock, getStock, delStock, addStock, updateStock } from "@/api/stock/stock";

export default {
  name: "Stock",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 股票信息表格数据
      stockList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        amount: null,
        amplitude: null,
        close: null,
        flowMarketValue: null,
        high: null,
        listingDate: null,
        low: null,
        open: null,
        preClose: null,
        stockName: null,
        stockNum: null,
        totalFlowShares: null,
        totalMarketValue: null,
        totalShares: null,
        turnOverrate: null,
        upDownPrices: null,
        upDownRange: null,
        upDownRange3: null,
        upDownRange5: null,
        updateDate: null,
        volume: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询股票信息列表 */
    getList() {
      this.loading = true;
      listStock(this.queryParams).then(response => {
        this.stockList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        stockId: null,
        amount: null,
        amplitude: null,
        close: null,
        flowMarketValue: null,
        high: null,
        listingDate: null,
        low: null,
        open: null,
        preClose: null,
        stockName: null,
        stockNum: null,
        totalFlowShares: null,
        totalMarketValue: null,
        totalShares: null,
        turnOverrate: null,
        upDownPrices: null,
        upDownRange: null,
        upDownRange3: null,
        upDownRange5: null,
        updateDate: null,
        volume: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.stockId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加股票信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const stockId = row.stockId || this.ids
      getStock(stockId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改股票信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.stockId != null) {
            updateStock(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStock(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const stockIds = row.stockId || this.ids;
      this.$modal.confirm('是否确认删除股票信息编号为"' + stockIds + '"的数据项？').then(function() {
        return delStock(stockIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('stock/stock/export', {
        ...this.queryParams
      }, `stock_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
